//
//  SFHomeViewController.swift
//  SFWeiboPro
//
//  Created by happy on 2017/9/6.
//  Copyright © 2017年 happy. All rights reserved.
//

import UIKit

private let homeCell = "homeCell"

class SFHomeViewController: SFBaseViewController {
    
    lazy var statusListViewModel = SFStatusListViewModel()
    
    override func loadData() {
        
            statusListViewModel.loadStatus(pullUp: self.isPullUp) { (isSuccess, shoudRefresh) in
        
            if isSuccess {
                //数据加载完,停止加载
                self.refreshControl?.endRefreshing()
                print("刷新表格")
                self.isPullUp = false
                //刷新数据列表
                if shoudRefresh {
                    self.tableView?.reloadData()
                }
            }
        }
        
    }

    @objc func showFriends() {
        print(#function)
        
        let vc = SFDemoViewController()
        
        navigationController?.pushViewController(vc, animated: true)
    }

}

extension SFHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.statusArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: homeCell, for: indexPath) as! SFStatusViewCell
        
        let viewModel = statusListViewModel.statusArray[indexPath.row]
        
        
        cell.viewModel = viewModel
        
        return cell
        
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 300
    }
    
}


extension SFHomeViewController {
    
    override func setupTabelView() {
        super.setupTabelView()
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        //注册cell
        tableView?.register(UINib(nibName: "SFStatusNormalViewCell", bundle: nil), forCellReuseIdentifier: homeCell)
        
        tableView?.rowHeight = UITableViewAutomaticDimension
        tableView?.estimatedRowHeight = 300
        
        tableView?.separatorStyle = .none
        
        setupNavTitle()
    }
    
    private func setupNavTitle() {
        
        let title = SFNetworkManager.shareManager.userAccount.screen_name
        
        let titleBtn = SFTitleButton(title: title)
        
        navItem.titleView = titleBtn
        
        titleBtn.addTarget(self, action: #selector(titleClick), for: .touchUpInside)
    }
    
    @objc func titleClick(btn: UIButton) {
        
        btn.isSelected = !btn.isSelected
        
    }
}
